<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp"%>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
<div class="container-fluid am-margin-top">
<form id="${uuid}_form">
	<input type="hidden" name="id">
	<input type="hidden" name="mainTableId">
	<input type="hidden" name="hasBuilt">
	<input type="hidden" name="tableName">
	<input type="hidden" name="dataModelGroupCode">

	<div class="form-group">
		<label>表描述</label>
		<input type="text" name="tableDesc" class="form-control" check-type="required">
	</div>
	<div class="form-group">
		<label>表编码</label>
		<input type="text" name="tableCode" class="form-control" check-type="required">
		<p class="help-block">物理表名='dm_'+表编码</p>
	</div>
	<div class="form-group" >
		<label>是否主表</label>
		<select name="isMain" class="form-control">
			<option value="1">是</option>
			<option value="0">否</option>
		</select>
	</div>
	<div class="form-group" id="${uuid}_mainTableSel" >
		<label>选择主表</label>
		<input type="text" name="mainTableName" class="form-control">
	</div>
	<div class="form-group" id="${uuid}_mainSlaveRelField" >
		<label>主从关联字段</label>
		<input type="text" name="mainSlaveRelField" class="form-control" value="REFID">
		<p class="help-block">在从表中与主表关联的字段，默认使用REFID</p>
	</div>
	<div class="form-group">
		<a href="javascript:void(0);" id="${uuid}_submitBt" class="btn btn-primary pull-right">提交</a>
	</div>
</form>
</div>

<script type="text/javascript">
$(function(){
	var id = '${param.id}';
	var dataModelGroupCode='${param.dataModelGroupCode}';
	
	var uuid = '${uuid}';
	var form = $("#" + uuid + "_form");
	var formValidation=form.validation();
	
	var mainTableSel=$("#"+uuid+"_mainTableSel");
	var mainSlaveRelField=$("#"+uuid+"_mainSlaveRelField");
	
	$("#" + uuid + "_submitBt").click(function(){
		if (isValid()) {
			form.find(":disabled").removeAttr("disabled");
			form.ajaxSubmit({
				url : ctx + '/formTableDefineAction/saveTableDef.do',
				data : {},
				type : "POST",
				success : function(data) {
					if (data.success) {
						webUtils.alert("提交成功");
						top.webUtils.topicPublish(
								"tableDefine.edit.success", data);
					} else {
						webUtils.error(data.msg);
					}
				}
			});
		}
	});
	
	function isValid(){
    	return formValidation.valid();
    }
	
	form.find("input[name=mainTableName]").click(function(){
		var $this=$(this);
		var sub = top.webUtils.topicSubscribe("selectMainTable.selected", function (data) {
            form.find("input[name=mainTableName]").val(data.tableName);
            form.find("input[name=mainTableId]").val(data.id);
			dialog.close();
        });
		var dialog= BootstrapDialog.show({
	            title: '关联主表',
	            message:$('<div></div>').load(ctx + '/formTableDefineAction/selectMainTableView.do'),
	            onhidden: function () {
	            	sub.remove();
	            }
	        });
	});
	
	form.find("select[name=isMain]").change(function(){
		var $this=$(this);
		var isMain=$this.val();
		changeOnIsMain(isMain);
	});
	
	form.find("input[name=tableDesc]").blur(function(){
		var $this=$(this);
		var tableCode=form.find("input[name=tableCode]")
		if(!tableCode.val()){
			webUtils.getShortPinyin($this.val(),function(str){
				tableCode.val(str);
			});
		}
	});
	
	var changeOnIsMain=function(isMain){
		if(isMain==0){
			mainTableSel.show();
			mainSlaveRelField.show();
		}else{
			mainTableSel.hide();
			mainSlaveRelField.hide();
			form.find("input[name=mainTableName]").val("");
            form.find("input[name=mainTableId]").val("");
		}
	}
	
	var init=function(){
		var isMain=form.find("select[name=isMain]").val();
		changeOnIsMain(isMain);
		if(id){
			form.find("input[name=mainTableName]").attr("disabled","disabled");
			form.find("select[name=isMain]").attr("disabled","disabled");
			form.find("input[name=mainSlaveRelField]").attr("disabled","disabled");
		}else{
			if(dataModelGroupCode){
				form.find("input[name=dataModelGroupCode]").val(dataModelGroupCode);
			}
		}
	}
	
	if (id) {
		$.getJSON(ctx + '/formTableDefineAction/findTableDefById.do',{id : id},function(data){
			webUtils.fullForm({
				form : form,
				data : data
			});
			
			init();
			
		});
	}else{
		init();
	}
	
});
</script>